﻿using Common.Contract.CrossSelling.Record;
using Common.Contract.CrossSelling.Request;
using Common.Contract.CrossSelling.Response;
using Common.Service.Command;
using CrossSelling.Model.Interface;
using CrossSelling.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;

namespace CrossSelling.Core.ServiceCommand
{
    public class RetrieveListHoldProductServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveListProductRequest, RetrieveListProductResponse>
    {
        #region Properties
        public string Dao;
        #endregion
        public RetrieveListHoldProductServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveListProductRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
        }

        protected override bool OnExecute(RetrieveListProductResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            List<XSellingProductRecord> records = new List<XSellingProductRecord>();

            List<ProductSMEByProductHolding> listProductItem = Providers.CrossSellingProducts.RetrieveListHoldProductCrossSelling();
            records = listProductItem.Select(x => new XSellingProductRecord { Product = x.Product, ProductName = Request.Lang.Equals("en") ? x.ProductNameEn : x.ProductName }).ToList();

            response.Records = records;
            return isSuccess;
        }
    }
}
